package com.biqee.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class JsoupUtil {
	private static final Logger logger = Logger.getLogger(JsoupUtil.class);
	
	public static Document createDocument(String htmlString) {
		Document doc = null;
		try {
			doc = Jsoup.parse(htmlString, "gbk");	
		} catch (Exception e) {
			logger.error("create doc failed.");
		}
		
		return doc;
	}
	
	public static  Document createDocument(String filePath, String charsetName) {
		File input = new File(filePath);
		Document doc = null;
		try {
			doc = Jsoup.parse(input, charsetName);
		} catch (IOException e) {
			logger.error("parse file failed. file: " + filePath +
					", charsetName: " + charsetName +
					", cause: ", e);
		}
		
		return doc;
	}
	
	/**
	 * 根据输入流生成Jsoup解析文档，请注意，本方法不会关闭输入流，调用方需要负责关闭输入流
	 * @param inputSream
	 * @param charsetName
	 * @param baseUrl
	 * @return
	 */
	public static Document createDocument(InputStream inputSream, String charsetName, String baseUrl) {
		Document doc = null;
		try {
			doc = Jsoup.parse(inputSream, charsetName, baseUrl);	
		} catch (Exception e) {
			logger.error("create doc failed.");
		}
		
		return doc;
	}
	
	

}
